package com.seeyon.apps.ebs.rcyh.timer.impl;

import com.alibaba.fastjson.JSONObject;
import com.seeyon.apps.ebs.rcyh.manager.RcyhManager;
import com.seeyon.apps.ebs.rcyh.timer.RcyhTimer;
import com.seeyon.ctp.common.AppContext;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import java.time.LocalDate;
import java.util.Calendar;
import java.util.Date;
import java.util.Timer;
import java.util.TimerTask;

public class RcyhTimerImpl implements RcyhTimer {
    private static Log log = LogFactory.getLog(RcyhTimerImpl.class);
    private static RcyhManager rcyhManager = (RcyhManager) AppContext.getBean("rcyhManager");


    @Override
    public void timer() {
        try {
            long interval = 10*60*1000; //定时器间隔时间---15分钟
            //long interval =60*60*1000;
            Calendar calendar = Calendar.getInstance();
            calendar.set(Calendar.HOUR_OF_DAY, 0); //24小时制、定时器启动时间
            calendar.set(Calendar.MINUTE, 0);
            calendar.set(Calendar.SECOND, 0);
            Date date=calendar.getTime(); //第一次执行定时任务的时间
            Timer timer = new Timer();
            timer.schedule(new TimerTask() {// 定时器
                public void run() {
                    LocalDate now = LocalDate.now();
                    JSONObject dataJson = new JSONObject();
                    dataJson.put("year",now.getYear());
                    rcyhManager.updateRcMiddleTable(dataJson);
                    log.info("EBS动迁优化---定时器---定时器启动时间 ："+now.toString());
                }
            }, date, interval);
        } catch (Exception e) {
            log.error("EBS动迁优化---定时器---定时器执行方法异常！！！",e);
        }
    }
}
